package logic

import (
	"context"
	"encoding/json"
	"fresh-box/cache"
	"fresh-box/model"
	"time"

	"fresh-box/freshrpc/freshrpc"
	"fresh-box/freshrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ListHemaLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewListHemaLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListHemaLogic {
	return &ListHemaLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ListHemaLogic) ListHema(in *freshrpc.ListHemaRequest) (*freshrpc.ListHemaResponse, error) {
	// todo: add your logic here and delete this line
	var hema []model.Hema
	result, _ := cache.Rdb.Get(cache.Ctx, "List").Result()
	if result == "" {
		model.DB.Debug().Table("hemas").
			Select("hemas.*,classes.`name` as class_name").
			Joins("LEFT join hemas on productList", result, time.Minute*30)
	} else {
		json.Unmarshal([]byte(result), &hema)
	}

	return &freshrpc.ListHemaResponse{}, nil
}
